
/**查询参数信息*/
var url = "/openx/goods/goodsServer/queryGoodsInfoForPage";
//对应接口入参
var param = {};

var formId = 'goodsInfoForm';

(function () {
    /**设置增删改查的工具条*/
    $.openx.setTableToolBar('goodsInfoBar');
    /**进行表格数据初始化*/
    var data = $.openx.getJsonObj(formId);
    param.bean = data;
    $.openx.initTable("table", url, param);

})();

function codeFormatter(value, row){
    return "<a   style = '\color:#000000;text-decoration:underline;\' onclick='javascript:queryDetail(" + JSON.stringify(row)+ ");'><b>"+row.goodsCode+"</b></a>";
}


function timeDateFormat(value) {
    console.log(value)
    return !value ? "-" : new Date(value).format("yyyy-MM-dd hh:mm")
}


$.openx.insert = function () {
    var createForm = "goodsInfoPartnerForm";

    var innerHtml = $.openx.innerHtml('goodsInfoDialog');
    laytpl(innerHtml).render({}, function (html) {
        $.openx.openLayer(['auto', 'auto'], '新增商品基本信息', html, false, function () {
            if ($.openx.validation(createForm)) {
                //获取表单数据
                var param = $.openx.getJsonObj(createForm);
                $X("/boss/goods/goodsServer/", "createGoodsInfo").callx({
                    param: param,
                    onResult: function (data) {
                        if (data) {
                            $.openx.tableRefresh(null, null, true);
                            layer.closeAll();
                            layer.msg("新增成功");
                        }
                    },
                    onError: function (error) {
                        var res = JSON.parse(error);
                        layer.msg(res.message)
                    }
                });
            }
        });
    })
}

function queryDetail(row){
    $X("/boss/goods/goodsServer", "queryDetail").callx({
        goodsCode: row.goodsCode,
        onResult: function (data) {
            if (data) {
                var innerHtml = $.openx.innerHtml('goodsDetail');
                laytpl(innerHtml).render(data, function (html){
                    $.openx.openLayer(null, '查询商品详情', html, false);
                });
            } else {
                layer.msg("无详情信息")
            }
        },
        onError: function (error) {
            var res = JSON.parse(error);
            layer.msg(res.message)
        }

    });
}

$.openx.update = function () {
    $.openx.onClickUpdate(function (rows) {
        var createForm = "updateGoodsInfoForm";

        var innerHtml = $.openx.innerHtml('updateGoodsInfoDialog');
        laytpl(innerHtml).render(rows[0], function (html) {
            $.openx.openLayer(['600', '770'], '修改商品基本信息', html, false, function () {
                if ($.openx.validation(createForm)) {
                    //获取表单数据
                    //var param = $.openx.getJsonObj(createForm);
                    var param = $('#' + createForm).serializeObject();
                    $X("/goods/goodsServer", "updateGoodsInfo").callx({
                        goodsInfo: param,
                        onResult: function (data) {
                            $.openx.tableRefresh(null, null, true);
                            layer.closeAll();
                            layer.msg("修改成功");
                        },
                        onError: function (error) {
                            var res = JSON.parse(error);
                            layer.msg(res.message)
                        }
                    });
                }
            });
        })
    })
}


$.fn.serializeObject = function serializeObject() {  
        var o = {};  
        var a = this.serializeArray();  
        $.each(a, function() {  
            if (o[this.name]) {  
                if (!o[this.name].push) {  
                    o[this.name] = [ o[this.name] ];  
                }  
                o[this.name].push(this.value || '');  
            } else {  
                o[this.name] = this.value || '';  
            }  
        });  
        return o;  
    }  

$.openx.delete = function () {
    $.openx.onClickDelete(function (rows) {
        console.log(rows)
    })
}

$.openx.import = function () {
    $("#import-form").modal("show");
}

$.openx.query = function () {
    var data = $.openx.getJsonObj(formId);
    param.bean = data;
    $.openx.tableRefresh(url, param);
}

function importExcel() {
    //判断文件大小
    checkFileSizeType('excelFileInput', 10 * 1024 * 1024, '');
    
    loadFileDataToBase64String('excelFileInput', function(excel){ 
        $("#importExcelButton").attr("disabled", true);
                
        $X("/boss/goods/goodsServer", "importGoodsInfo").callx({
            base64Str : excel,
            onResult : function(data) {
                $("#importExcelButton").attr("disabled", false);
                if(data && data.length > 0) { 
                    $('#uploadImgResult').html(data);
                } else {
                    $('#uploadImgResult').html("已加入导入队列!!!");
                }
           },
           //请求失败执行
           onError : function(error) {
               $("#importExcelButton").attr("disabled", false);
               var res = JSON.parse(error);
               $('#uploadImgResult').html(res.message);
               layer.msg(res.message)
           }
       }, 3000*1000);
    }); 
}

function checkFileSizeType(fileId, maxFileSize, fileTypeArr) {
    var file = document.getElementById(fileId).files[0];
    if (!file) {
        layer.tips("请选择文件！","#excelFileInput",{
            tips:[2,"#f40"]
        })
        return -1;
    }
    //检查文件类型
    if (fileTypeArr && fileTypeArr.length > 0) {
        for (var i = 0; i < fileTypeArr.length; i++) {
            fileTypeArr[i] = fileTypeArr[i].toLowerCase();
        }
        if (fileTypeArr.indexOf(file.name.substring(file.name.lastIndexOf("."), file.name.length).toLowerCase()) === -1) {
            layer.tips("上传文件类型错误！","#excelFileInput",{
                tips:[2,"#f40"]
            })
            return -1;
        }
    }
    //检查文件大小
    if (file.size > maxFileSize) {
        layer.tips("上传文件不能大于" + (maxFileSize / 1024 ).toFixed(2) + "KB！","#excelFileInput",{
            tips:[2,"#f40"]
        })
        return -1;
    }
}

function loadFileDataToBase64String(fileName, callback) {
    var file = document.getElementById(fileName).files[0];
    //读取文件
    var reader = new FileReader();
    reader.onload = function () {
        var result = this.result.split('base64,')[1];
        callback(result);
    }
    reader.readAsDataURL(file);
}